package nix.ajax.web.servlet;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CityServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		// 设置XML的Content-Type响应头
		response.setContentType("text/xml;charset=utf-8");

		String province = request.getParameter("province");

		try {
			SAXReader reader = new SAXReader();
			InputStream input = this.getClass().getResourceAsStream(
					"/nix/ajax/web/servlet/china.xml");
			Document doc = reader.read(input);

			Element ele = (Element) doc.selectSingleNode("//province[@name='" + province + "']");
			// 把元素转换为XML字符串
			String xml = ele.asXML();
			response.getWriter().print(xml);

		} catch (DocumentException e) {
			throw new RuntimeException(e);
		}
	}

}
